
import pandas as pd

# 清洗空值
# 通过 isnull() 判断各个单元格是否为空
def fun1():
    df = pd.read_csv('../property-data.csv')

    print(df['NUM_BEDROOMS'])
    print(df['NUM_BEDROOMS'].isnull())

# 指定空数据类型
def fun2():
    missing_values = ['n/a', 'na', '--']
    df = pd.read_csv('../property-data.csv', na_values=missing_values)

    print(df['NUM_BEDROOMS'])
    print(df['NUM_BEDROOMS'].isnull())

# 删除包含空数据的行
def fun3():
    df = pd.read_csv('../property-data.csv')

    new_df = df.dropna()
    print(new_df.to_string())

# 使用implace=True可以修改元数据DataFrame
def fun4():
    df = pd.read_csv('../property-data.csv')
    df.dropna(inplace=True)
    print(df.to_string())

# 移除ST_NUM列中字段值为空的行
def fun5():
    df = pd.read_csv('../property-data.csv')
    df.dropna(subset=['ST_NUM'], inplace=True)
    print(df.to_string())

# 使用12345替换空字段
def fun6():
    df = pd.read_csv('../property-data.csv')
    df.fillna(12345, inplace=True)
    print(df.to_string())

# 使用12345替换PID为空数据
def fun7():
    df = pd.read_csv('../property-data.csv')
    df['PID'].fillna(12345, inplace=True)
    print(df.to_string())

# 使用mean()方法计算列的均值并替换空单位格
def fun8():
    df = pd.read_csv('../property-data.csv')
    x = df['ST_NUM'].mean()
    df['ST_NUM'].fillna(x, inplace=True)
    print(df.to_string())

# 使用median()方法计算列的中位数并替换空单位格
def fun9():
    df = pd.read_csv('../property-data.csv')
    x = df['ST_NUM'].median()
    df['ST_NUM'].fillna(x, inplace=True)
    print(df.to_string())

# 使用mode()方法计算列的众数并替换空单位格
def fun10():
    df = pd.read_csv('../property-data.csv')
    x = df['ST_NUM'].mode()
    df['ST_NUM'].fillna(x, inplace=True)
    print(df.to_string())

# 清洗格式错误数据
# 格式化日期
def fun11():
    # 第三个日期格式错误
    data = {
        "Date":['2020/12/01','2020/12/02','20201226'],
        "duration":[50, 40, 45]
    }
    df = pd.DataFrame(data, index=["day1", "day2", "day3"])
    df['Date'] = pd.to_datetime(df['Date'])
    print(df.to_string())

# 清洗错误数据
def fun12():
    person = {
        "name": ['Google', 'Runoob', 'Taobao'],
        "age": [20, 50, 13234]
    }

    df = pd.DataFrame(person)
    df.loc[2, 'age'] = 30 # 修改数据
    print(df.to_string())

# 清洗重复数据
# 数据重复，duplicated() 会返回 True
def fun13():
    person = {
        "name": ['Google', 'Runoob', 'Runoob', 'Taobao'],
        "age": [20, 40, 40, 23]
    }
    df = pd.DataFrame(person)

    print(df.duplicated())

# 删除重复数据
def fun14():
    person = {
        "name": ['Google', 'Runoob', 'Runoob', 'Taobao'],
        "age": [20, 40, 40, 23]
    }
    df = pd.DataFrame(person)
    df.drop_duplicates(inplace=True)
    print(df)

if __name__ == '__main__':
    fun14()